package com.sduept.nwld.dataserver.manager.etl;

import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.entity.FaultRecordWrapper;
import com.sduept.bigdata.fault.enums.RegexTypeEnum;
import com.sduept.bigdata.fault.parser.entity.StandardLibrary;
import com.sduept.cim.ledger.manager.SubstationLedgerManager;
import com.sduept.cim.ledger.model.SubstationLedger;
import com.sduept.cim.model.core.Substation;
import com.sduept.cim.model.wires.BusbarSection;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.model.daily.Runlog;
import com.sduept.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 母线故障记录抽取
 * 
 * @author Author
 *
 */
@Service
public class BusFaultDataETLManager extends FaultETLDataManager {

	private static final String SIGN_BUS = "母线";
	
	@Autowired
	private SubstationLedgerManager stationLedgerM;

	/**
	 * 母线故障记录抽取
	 */
	public void extracFaultRecord(Runlog record, Map<String, List<StandardLibrary>> map) throws Exception {

		List<StandardLibrary> list = map.get(RegexTypeEnum.BUS_FAULT_REGEX.getCode());
		List<StandardLibrary> timeList = map.get(RegexTypeEnum.SIGN_FAULT_TIME_REGEX.getCode());
		String strs[] = record.getLog_contents().split(DEFAULT_SPLIT_REGEX);
		for (String str : strs) {
			if (!FaultRegexUtil.isMatcher(str, list)) {
				continue;
			}
			FaultRecord fault = new FaultRecord();
			fault.setSource(4);
			// 获取故障时间
			Date faultDate = getFaultTime(str, record.getTime(), timeList);
			if (null == faultDate) {
				continue;
			}
			fault.setId(ObjectId.get().toString());
			fault.setFaultTime(faultDate);
			// 设置类型
			fault.setEtype(SIGN_BUS);
			// 设置电压等级
			String v = find1("([0-9]+)kV", str);
			fault.setVoltage(v.equals("") ? null : Integer.parseInt(v));
			// 设置故障设备及设备ID
			Substation sub = null;
			List<Substation> listSubstations = cache.getS500List();
			for (Substation station : listSubstations) {
				if (record.getLog_contents().contains(station.getName())) {
					sub = station;
					break;
				}
			}
			if (sub == null) {
				continue;
			}
			if (fault.getVoltage() == null) {
				fault.setVoltage((int) sub.getStandVoltagelevel());
			}
			fault.setStationId(sub.getId());
			fault.setStationName(sub.getName());
			List<StandardLibrary> list2 = map.get(RegexTypeEnum.BUS_DATA_REGEX.getCode());
			String s = getFirstMatcher(record.getLog_contents(), list2);
			s = s.replaceAll("[^\\d]", "");
			List<BusbarSection> busbars = cm.getBusbarSectionsByStationId(sub.getId());
			for (BusbarSection busbar : busbars) {
				String busbarName = busbar.getName().replaceAll(SIGN_VOLTAGE_REGEX, "").replaceAll("[^\\d]", "");
				if (StringUtils.isNotEmpty(busbarName) && StringUtils.isNotEmpty(s) && s.equals(busbarName)) {
					if (busbar.getStandVoltagelevel() > 499 && busbar.getStandVoltagelevel() == fault.getVoltage()) {
						fault.setEquipment_id(busbar.getId());
						fault.setEquipment(sub.getName() + busbar.getName());
						break;
					}
				}
			}
			if (StringUtils.isEmpty(fault.getEquipment())) {
				continue;
			}
			// 判断是否存储
			if (fault.getVoltage() == null || fault.getVoltage() < 500) {
				continue;
			}
			if (StringUtils.isEmpty(fault.getRank()) && StringUtils.isNotEmpty(fault.getStationId())) {
				SubstationLedger stationLedger = stationLedgerM.findSubstationLedger(fault.getStationId());
				if(null != stationLedger) {
					fault.setRank(stationLedger.getPrimaryEquipmentScope());
				}
			}
			fm.saveFaultRecord(new FaultRecordWrapper(fault));
		}

	}

	/**
	 * 母线故障原因抽取
	 */
	public void extracFaultResult(Runlog record, Map<String, List<StandardLibrary>> map) throws Exception {
		List<StandardLibrary> list = map.get(RegexTypeEnum.SIGN_BUS_RESULT_REGEX.getCode());
		if (!FaultRegexUtil.isMatcher(record.getLog_contents(), list)) {
			return;
		}
		String strs[] = record.getLog_contents().split(DEFAULT_SPLIT_REGEX);
		List<StandardLibrary> timeLibs = map.get(RegexTypeEnum.SIGN_BUS_FAULT_REASON_TIME.getCode());
		List<StandardLibrary> tmplibs = map.get(RegexTypeEnum.BUS_DATA_REGEX.getCode());
		for (String str : strs) {
			if (!FaultRegexUtil.isMatcher(str, list)) {
				continue;
			}
			FaultRecord fault = new FaultRecord();
			fault.setSource(4);
			String date = getFirstMatcher(str, timeLibs);
			if (StringUtils.isEmpty(date)) {
				continue;
			} else {
				date = getDateByString(date);
			}
			String faultDate = getCurrentYear(record.getTime()) + "-" + date + "00:00:00";
			Date faultTime = DateUtil.getDateByString(faultDate, DateUtil.pattern_ymdhms);
			fault.setFaultTime(faultTime);
			// 设置类型
			fault.setEtype(SIGN_BUS);
			// 设置电压等级
			String v = find1("([0-9]+)kV", record.getLog_contents());
			fault.setVoltage(v.equals("") ? null : Integer.parseInt(v));
			// 设置故障设备及设备ID
			Substation sub = null;
			List<Substation> listSubstations = cache.getS500List();
			for (Substation station : listSubstations) {
				if (record.getLog_contents().contains(station.getName().replaceAll("变", "").replaceAll("站", ""))) {
					sub = station;
					break;
				}
			}
			if (sub != null) {
				if (fault.getVoltage() == null) {
					fault.setVoltage((int) sub.getStandVoltagelevel());
				}
				fault.setStationId(sub.getId());
				fault.setStationName(sub.getName());
				String s = getFirstMatcher(str, tmplibs);
				s = s.replaceAll("[^\\d]", "");
				List<BusbarSection> busbars = cm.getBusbarSectionsByStationId(sub.getId());
				for (BusbarSection busbar : busbars) {
					String busbarName = busbar.getName().replaceAll(SIGN_VOLTAGE_REGEX, "").replaceAll("[^\\d]", "");
					if (StringUtils.isNotEmpty(busbarName) && StringUtils.isNotEmpty(s) && s.equals(busbarName)) {
						if (busbar.getStandVoltagelevel() > 499 && busbar.getStandVoltagelevel() == fault.getVoltage()) {
							fault.setEquipment_id(busbar.getId());
							fault.setEquipment(sub.getName() + busbar.getName());
							break;
						}
					}
				}
				if (StringUtils.isEmpty(fault.getEquipment())) {
					continue;
				}
				// 判断是否存储
				if (fault.getVoltage() == null || fault.getVoltage() < 500) {
					continue;
				}
				List<FaultRecord> condition = null;
				Date[] dates = getOneDates(fault.getFaultTime());
				if (StringUtils.isNotEmpty(fault.getEquipment_id())) {
					condition = fqManager.findFaultByTimeDeviceId(dates[0], dates[1], fault.getEquipment_id());
				} else {
					condition = fqManager.findFaultByTimeDeviceName(fault.getEquipment(), dates[0], dates[1]);
				}
				if (condition.size() > 0) {
					for (FaultRecord tmp : condition) {
						if (StringUtils.isNotEmpty(tmp.getRemark())) {
							continue;
						}
						tmp.setRemark(record.getLog_contents());
						dao.createOrUpdate(tmp, FaultRecord.class);
					}
				}
			}
		}
	}

}
